//
//  StatusCellTopView.swift
//  mySina
//
//  Created by mac on 15/12/21.
//  Copyright © 2015年 tz. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage
class StatusCellTopView: UIView {
    //定义 视图的底部约束的属性
    var bottomConstraints: Constraint?
    //定义微博模型数据
    var status:Status? {
        didSet {
            iconImage.sd_setImageWithURL(status?.user?.headURL)
            nameLabel.text = status?.user?.name
            mbrankImage.image = status?.user?.mbrank_image
            verified_type_image.image = status?.user?.verified_type_image
            //TODO: 后续完善
//            timeLabel.text = status?.created_at
//            sourceLabel.text = status?.source
            contentLabel.text = status?.text
            //配图视图
            pictureView.imageUrls = status?.imageURLs
            //在更新约束之前，将之前的约束关系先卸载掉
            self.bottomConstraints?.uninstall()
            //根据是否需要配图，来动态的调整约束关系
            if let urls = status?.imageURLs where urls.count > 0 {
                //配图视图  在一个 {}内 对应的闭包 无法简写
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstraints = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMarigin).constraint
                })
            } else {
                //没有配图
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstraints = make.bottom.equalTo(contentLabel.snp_bottom).offset(StatusCellMarigin).constraint
                })
            }
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        //设置背景色
        backgroundColor = UIColor(white: 0.9, alpha: 1)
        setupUI()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //自定义顶部视图
    private func setupUI() {
        //添加子视图
        addSubview(iconImage)
        addSubview(nameLabel)
        addSubview(mbrankImage)
        addSubview(verified_type_image)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(pictureView)
        addSubview(sepView)
        //添加约束
        sepView.backgroundColor = UIColor.orangeColor()
        sepView.snp_makeConstraints { (make) -> Void in
            make.top.right.left.equalTo(self)
            make.height.equalTo(10)
        }
        iconImage.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(sepView.snp_bottom)
            make.left.equalTo(self).offset(StatusCellMarigin)
            make.size.equalTo(CGSize(width: StatusCellHeadImageWidth, height: StatusCellHeadImageWidth))
        }
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(iconImage.snp_right).offset(StatusCellMarigin)
            make.top.equalTo(iconImage.snp_top)
        }
        mbrankImage.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(nameLabel.snp_top)
            make.left.equalTo(nameLabel.snp_right).offset(StatusCellMarigin)
        }
        verified_type_image.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconImage.snp_right)
            make.centerY.equalTo(iconImage.snp_bottom)
        }
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(iconImage.snp_bottom)
            make.left.equalTo(iconImage.snp_right).offset(StatusCellMarigin)
        }
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(timeLabel.snp_bottom)
            make.left.equalTo(timeLabel.snp_right).offset(StatusCellMarigin)
        }
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconImage.snp_bottom).offset(StatusCellMarigin)
            make.left.equalTo(iconImage.snp_left)
        }
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom).offset(StatusCellMarigin)
            make.left.equalTo(contentLabel.snp_left)
        }
        //对顶部视图的底部设置约束
        self.snp_makeConstraints { (make) -> Void in
            self.bottomConstraints = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMarigin).constraint
        }
    }
    //懒加载所有的子视图
    //分割线视图
    private lazy var sepView:UIView = UIView()
    //用户图像
    private lazy var iconImage:UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    //用户呢称
    //应为在重写label中已经给margin赋值了，所以不写会默认设置为0
    private lazy var nameLabel:UILabel = UILabel(title: "傻中傻", color: themeColor, fontSize: 14)
    //会员等级图像
    private lazy var mbrankImage:UIImageView = UIImageView(image: UIImage(named: "common_icon_membership_level1"))
    //认证类型
    private lazy var verified_type_image:UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    //时间
    private lazy var timeLabel:UILabel = UILabel(title: "1.11", color: themeColor, fontSize: 10, margin: 0)
    //发送者
    private lazy var sourceLabel:UILabel = UILabel(title: "呵呵", color: themeColor, fontSize: 10, margin: 0)
    //消息
    private lazy var contentLabel:UILabel = UILabel(title: "讲得好不好的时刻把客户对不上课活动我骄傲的干部阿是个哈工大别逼我了世界上开始觉得不合适了", color: themeColor, fontSize: 14, margin: StatusCellMarigin)
    //配图视图
    private lazy var pictureView:StatusPictureView = StatusPictureView()
}
